package com.mimiwang.animal.controller;

import com.github.pagehelper.PageInfo;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.maxmind.geoip2.model.CityResponse;
import com.mimiwang.animal.constant.RedisConstant;
import com.mimiwang.animal.service.IAnimalService;
import com.mimiwang.animal.vo.AnimalInfoVo;
import com.mimiwang.common.aop.LoginAspect;
import com.mimiwang.common.entity.school.School;
import com.mimiwang.common.entity.school.SchoolArea;
import com.mimiwang.common.entity.school.SchoolCampus;
import com.mimiwang.common.utils.IPUtil;
import com.mimiwang.common.utils.ResponseUtil;
import com.mimiwang.common.vo.school.PageConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @AUTHOR:游李滨
 * @DATE:2023/4/19 16:22
 */

@RestController
@RequestMapping("/animal")
public class AnimalController {

    @Autowired
    private ApplicationContext applicationContext;


    @Autowired
    IAnimalService animalService;


//    @RequireLogin
//    @Limit(key = "aniInfo",num = 3,duration = 20,timeUnit = TimeUnit.SECONDS)
    @GetMapping("/info/id/{id}")
    public String getAnimalById(@PathVariable long id){

        return ResponseUtil.successJSONData(animalService.selectAnimalById(id));
    }
    @PostMapping("/list/school/{pageNum}")
    public String getAnimalListBySchool(@RequestBody AnimalInfoVo animalInfoVo,
                                        @PathVariable int pageNum){
        School school = animalInfoVo.getSchool();
        SchoolCampus campus = animalInfoVo.getCampus();
        SchoolArea area = animalInfoVo.getArea();
        PageConfig pageConfig = new PageConfig(pageNum);
        return ResponseUtil.successJSONData(animalService.selectAnimalListBySchool(school.getId(),campus.getId(),area.getId(),pageConfig));
    }


    @GetMapping("/info/all/random")
    public String getRandomAnimalList(PageConfig pageConfig){
        PageInfo<AnimalInfoVo> animalInfoVoPageInfo = animalService.selectAllAnimal(pageConfig);
        return ResponseUtil.successJSONData(animalInfoVoPageInfo);
    }

    @GetMapping("/info/all/hot")
    public String getHotAnimalList(){
        List<Object> animalInfoVos = animalService.selectAnimalListByRedisKey(RedisConstant.HOT_ANIMAL_KEY);
        return ResponseUtil.successJSONData(animalInfoVos);
    }

    @GetMapping("/info/all/around")
    public String getAroundAnimalList(HttpServletRequest request,String ip){
        String ipAddr;
        if(ip == null){
            ipAddr = IPUtil.getIpAddr(request);
        }else{
            ipAddr=ip;
        }

        List<AnimalInfoVo> objects = animalService.selectAroundAnimalList(ipAddr);

        return ResponseUtil.successJSONData(objects);
    }

}
